package com.lmk.org.service.Impl;

import com.lmk.basic.service.Impl.BaseServiceImpl;
import com.lmk.basic.util.Md5Utils;
import com.lmk.basic.util.StrUtils;
import com.lmk.org.domain.Employee;
import com.lmk.org.mapper.EmployeeMapper;
import com.lmk.org.service.EmployeeService;
import com.lmk.user.domain.Logininfo;
import com.lmk.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void add(Employee employee) {
        //1.处理密码和盐值
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);


        System.out.println(logininfo.getId());
        employee.setLogininfo_id(logininfo.getId());

        super.add(employee);

        if (employee.getRole_id()!=null){
            //保存中间表的信息
            employeeMapper.addEmployeeRole(employee);
        }

    }
    public Logininfo employee2Logininfo(Employee employee){
        Logininfo logininfo = new Logininfo();

        BeanUtils.copyProperties(employee,logininfo);
        //当前对象是店铺管理员
        logininfo.setType(0);
        return logininfo;
    }

    @Override
    @Transactional
    public void del(Long id) {
        Long logininfo_id = employeeMapper.loadById(id).getLogininfo_id();
        //删除登录信息表数据
        //删除登录信息对象
        logininfoMapper.remove(logininfo_id);
        // 删除中间关联表信息
        employeeMapper.delEmployeeRoleByEmpId(id);
        super.del(id);

    }

    @Override
    @Transactional
    public void update(Employee employee) {
        //1.处理密码和盐值
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        Logininfo logininfo = employee2Logininfo(employee);
        //查询员工的Logininfo_id
        Long logininfo_id = employeeMapper.loadById(employee.getId()).getLogininfo_id();
        System.out.println(logininfo_id);
        logininfo.setId(logininfo_id);

        logininfoMapper.update(logininfo);
        //登录信息id和店铺信息id不能修改
        super.update(employee);

        //先删除以前的中间数据
        employeeMapper.delEmployeeRoleByEmpId(employee.getId());
        if (employee.getRole_id()!=null){
            //再添加新的数据
            employeeMapper.addEmployeeRole(employee);
        }



    }

    @Override
    public List<Employee> findByShopId(Long id) {
        return employeeMapper.findByShopId(id);
    }
}
